Skip to content

perf: avoid serialized copies of large worker buffers - #211

Merged
steipete merged 2 commits into
mainfrom
fix/large-buffer-postmessage-trailers
Oct 25, 2026
Merged

steipete merged 2 commits into
mainfrom
fix/large-buffer-postmessage-trailers

Conversation

@steipete

@steipete steipete commented Oct 25, 2026 •

Copy link
Copy Markdown

Large Worker messages copy ArrayBuffer payloads into the serialized byte vector, can copy that vector again when closing a container, and copy the payload into the receiver buffer. Use existing transferred-contents storage to hold one copy for a single-recipient send, then let the receiver adopt it.

Opt Worker and MessagePort sends into this path for fixed, nonshared ArrayBuffers of at least 16 KiB with no explicit transfer list. The source remains attached. Reuse the existing backing-index map, available because the mode excludes explicit transfers, without changing object-pool indexes. Broadcast serialization remains reusable; shared/resizable buffers and explicit transfers retain their handling. Stored and cross-process serialized bytes remain unchanged.

Qualified head: 20d214295042b7de88d5f0f9074fc067d5514289. The unchanged twelve-cell benchmark passes across three distinct quiet, on-demand Linux hosts, with ten interleaved pairs per cell and all 30 pairs retained. A/B used c7i.4xlarge; C used c7a.8xlarge after a c7i allocation timeout, before any C measurement. The separate session guard is 0.717122772 (limit 1.00). All 810 sample/control processes passed checksum/count and natural-exit checks.

Operation Payload Pooled fixed/S7 Limit
clone buffer-16k 0.989531117 1.03
clone buffer-256k 1.008542511 1.03
clone buffer-4m 0.964213527 1.03
clone session-entry 0.860317995 1.03
clone hook-payload 0.941716534 1.03
clone cause-free-error 0.969317304 1.03
postMessage buffer-16k 0.989151800 1.03
postMessage buffer-256k 0.696023867 1.03
postMessage buffer-4m 0.520228159 1.03
postMessage session-entry 0.702106070 1.03
postMessage hook-payload 0.845295119 1.03
postMessage cause-free-error 1.002056585 1.03

The first implementation failed the complete pool's small-error send cell at 1.036739333. Its observations are preserved. The follow-up removes the extra per-message map and receives the separate passing full pool above. No failed performance observation was rerun for green. A reservation-only diagnostic also failed; avoiding the trailer recopy alone was insufficient.

Validation on the qualified head:

  • 235 focused native tests passed, 7,748 assertions, six files.
  • All 288 wire cases and 250,571,955 bytes per runtime match the S7 baseline; clone/Worker/MessagePort and both cross-deserialization directions pass.
  • All three actual production IPC children pass in both directions with clean exits. Fixed-schema boundaries use the documented full-corpus sentinel plus real protocol requests.
  • Node 24 agrees on source preservation, repeated backing identity, failed sends and independent broadcasts.
  • Independent Codex P2 reviews of the optimization and map-reuse follow-up are complete and scoped-clean.
  • Exact-head native CI 40200109336 passes Linux x64 and macOS arm64; lint and formatting pass.

The earlier wider native sweep had 775 passes, 47 existing skips and one pre-existing Worker entry-point builtin-alias diagnostic mismatch, reproduced with both published baselines. Its assertion and outcome remain unchanged.

Related upstream work: oven-sh/bun#46714 uses a different trailer-reservation approach. The measured slowdown is inside serialization's large-vector copy path; no unique upstream causative line within the broad sync was isolated. The separate macOS vmForks watchdog failure is not established to share this cause or be fixed by this change. No merge or release is included.

Copy large fixed ArrayBuffers into existing ArrayBufferContents storage for
single-recipient Worker and MessagePort sends. Move the copied contents to
the receiver without detaching the source or serializing its payload again.

Keep broadcast, stored and cross-process serialization reusable and preserve
explicit-transfer handling. Cover shared backing references, marked source
buffers, failed sends and independent broadcast copies.
Avoid constructing and destroying a second hash map for every serialized
message. Single-recipient cloned contents require an empty transfer list,
so their backing indexes can use the existing transfer map without
changing object-pool numbering or detaching the source buffer.
@steipete
steipete marked this pull request as ready for review October 25, 2026 15:43
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T15:48:52.262352Z 20d2142 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@steipete
steipete merged commit 5534724 into main Oct 25, 2026
6 checks passed
@steipete
steipete deleted the fix/large-buffer-postmessage-trailers branch October 25, 2026 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant